package com.seven.controller;

import com.seven.domain.DropdownVO;
import com.seven.domain.Response;
import com.seven.domain.UserGroupInfo;
import com.seven.resource.GroupInfoResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/user-info")
@Api(tags = "组织信息")
public class UserInfoController {

  @Autowired
  private GroupInfoResource groupInfoResource;

  @GetMapping(value = "/groups")
  @ApiOperation("获取组织信息")
  public Response<DropdownVO<String>> groupList() {
    List<String> groupList = groupInfoResource.getGroupList();
    List<DropdownVO<String>> dropdownVOList = groupList.stream().map(t -> DropdownVO.toVo(t, t))
        .collect(Collectors.toList());
    return Response.successList(dropdownVOList);
  }

  @GetMapping(value = "/users/{groupName}")
  @ApiOperation("获取用户信息")
  public Response<DropdownVO<UserGroupInfo>> userList(@PathVariable("groupName") String groupName) {
    List<UserGroupInfo> groupUserList = groupInfoResource.getGroupUserList(groupName);
    List<DropdownVO<UserGroupInfo>> dropdownVOList = groupUserList.stream()
        .map(t -> DropdownVO.toVo(t, t.getUserName() + "+" + t.getUserCode()))
        .collect(Collectors.toList());
    return Response.successList(dropdownVOList);
  }

}
